/**
 * 工具类库统一入口文件
 * 提供统一的工具类访问接口
 */

// 确保基础类可用
if (typeof ApiClient === 'undefined') {
    console.error('ApiClient类未加载，请确保先加载ApiClient.js');
}
if (typeof CacheManager === 'undefined') {
    console.error('CacheManager类未加载，请确保先加载CacheManager.js');
}
if (typeof ErrorHandler === 'undefined') {
    console.error('ErrorHandler类未加载，请确保先加载ErrorHandler.js');
}

/**
 * 工具类工厂
 * 提供统一的工具类创建和配置接口
 */
class UtilsFactory {
    constructor() {
        this.instances = new Map();
        this.config = {
            api: {
                baseUrl: '',
                timeout: 30000,
                debug: false
            },
            cache: {
                storage: 'localStorage',
                defaultTTL: 300000,
                prefix: 'app_cache_',
                debug: false
            },
            error: {
                debug: false,
                enableLogging: true,
                enableNotifications: true,
                enableReporting: false
            }
        };
    }

    /**
     * 配置工具类
     */
    configure(config) {
        this.config = {
            api: { ...this.config.api, ...config.api },
            cache: { ...this.config.cache, ...config.cache },
            error: { ...this.config.error, ...config.error }
        };
        return this;
    }

    /**
     * 创建或获取ApiClient实例
     */
    getApiClient(options = {}) {
        const key = 'apiClient';
        if (!this.instances.has(key)) {
            const config = { ...this.config.api, ...options };
            const client = new ApiClient(config);

            // 设置其他工具类的引用
            client.cacheManager = this.getCacheManager();
            client.errorHandler = this.getErrorHandler();

            this.instances.set(key, client);
        }
        return this.instances.get(key);
    }

    /**
     * 创建或获取CacheManager实例
     */
    getCacheManager(options = {}) {
        const key = 'cacheManager';
        if (!this.instances.has(key)) {
            const config = { ...this.config.cache, ...options };
            this.instances.set(key, new CacheManager(config));
        }
        return this.instances.get(key);
    }

    /**
     * 创建或获取ErrorHandler实例
     */
    getErrorHandler(options = {}) {
        const key = 'errorHandler';
        if (!this.instances.has(key)) {
            const config = { ...this.config.error, ...options };
            const handler = new ErrorHandler(config);

            // 设置其他工具类的引用
            handler.notificationManager = window.App?.notificationManager || null;

            this.instances.set(key, handler);
        }
        return this.instances.get(key);
    }

    /**
     * 创建工具类实例
     */
    create(type, options = {}) {
        switch (type) {
            case 'api':
                return new ApiClient({ ...this.config.api, ...options });
            case 'cache':
                return new CacheManager({ ...this.config.cache, ...options });
            case 'error':
                return new ErrorHandler({ ...this.config.error, ...options });
            default:
                throw new Error(`Unknown utility type: ${type}`);
        }
    }

    /**
     * 获取所有实例
     */
    getAll() {
        return {
            api: this.getApiClient(),
            cache: this.getCacheManager(),
            error: this.getErrorHandler()
        };
    }

    /**
     * 销毁所有实例
     */
    destroy() {
        this.instances.forEach((instance, key) => {
            if (instance.destroy && typeof instance.destroy === 'function') {
                instance.destroy();
            }
        });
        this.instances.clear();
    }

    /**
     * 重置工厂
     */
    reset() {
        this.destroy();
        this.config = {
            api: {
                baseUrl: '',
                timeout: 30000,
                debug: false
            },
            cache: {
                storage: 'localStorage',
                defaultTTL: 300000,
                prefix: 'app_cache_',
                debug: false
            },
            error: {
                debug: false,
                enableLogging: true,
                enableNotifications: true,
                enableReporting: false
            }
        };
    }
}

// 创建全局工厂实例
window.UtilsFactory = UtilsFactory;

// 创建默认实例
window.utils = new UtilsFactory();

// 为了向后兼容，也直接暴露类
window.ApiClient = ApiClient;
window.CacheManager = CacheManager;
window.ErrorHandler = ErrorHandler;

// 提供便捷的全局访问方法
window.createApiClient = (options) => new ApiClient(options);
window.createCacheManager = (options) => new CacheManager(options);
window.createErrorHandler = (options) => new ErrorHandler(options);